<html>
<head><meta charset="utf-8"><title>Understanding tuple layout · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding.20tuple.20layout.html">Understanding tuple layout</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="187198560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding%20tuple%20layout/near/187198560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding.20tuple.20layout.html#187198560">(Feb 02 2020 at 09:45)</a>:</h4>
<p>Hi,<br>
I read the explanation(<a href="https://rust-lang.github.io/unsafe-code-guidelines/layout/structs-and-tuples.html#tuple-types" target="_blank" title="https://rust-lang.github.io/unsafe-code-guidelines/layout/structs-and-tuples.html#tuple-types">https://rust-lang.github.io/unsafe-code-guidelines/layout/structs-and-tuples.html#tuple-types</a>) of tuples layout and I want to make sure I understand it correctly.</p>
<p>does it mean that this is sound code? <code>transmute::&lt;(bool, &amp;u8), _&gt;((true, &amp;6u8));</code><br>
Can I be confident that all tuples of the same "type" will have the same memory layout and can be safely casted to raw pointers, pass through ffi but only dereferenced on the rust side?<br>
I know this is true in structs only as long as you use literally the same struct (ie 2 different structs can have different memory alignment and ordering even if they look the same).</p>
<p>So this is isn't sound: <code>struct A&lt;'a&gt; {b: bool, u: &amp;'au8}; struct B&lt;'a&gt; {b: bool, u: &amp;'au8}; transmute::&lt;B, A&gt;(A{b: true, a: &amp;7u8});</code></p>



<a name="187206644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding%20tuple%20layout/near/187206644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Köln <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding.20tuple.20layout.html#187206644">(Feb 02 2020 at 14:06)</a>:</h4>
<p>The transmuting of A and B definitely isn't sound. Passing a pointer to a type and then turning that into a reference of the same type should be sound. Including any tuple type.</p>



<a name="187221628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding%20tuple%20layout/near/187221628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding.20tuple.20layout.html#187221628">(Feb 02 2020 at 21:51)</a>:</h4>
<p><span class="user-mention" data-user-id="257700">@Sebastian Köln</span> hehe, I tried adding things that has special soundness requirements and haven't thought about the storing ref to a temp.</p>
<p>But what you're saying is that 2 tuples with the same data <em>are the same type</em></p>



<a name="187221679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding%20tuple%20layout/near/187221679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Köln <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding.20tuple.20layout.html#187221679">(Feb 02 2020 at 21:52)</a>:</h4>
<p>yes, (T, U) == (T, U)</p>



<a name="187221696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding%20tuple%20layout/near/187221696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Köln <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding.20tuple.20layout.html#187221696">(Feb 02 2020 at 21:53)</a>:</h4>
<p>the guidelines you linked make that very clear: </p>
<blockquote>
<p>an anonymous tuple type (T1..Tn) of arity N is laid out "as if" there were a corresponding tuple struct declared in libcore</p>
</blockquote>



<a name="187221735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding%20tuple%20layout/near/187221735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Köln <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding.20tuple.20layout.html#187221735">(Feb 02 2020 at 21:54)</a>:</h4>
<p>so unless you manage to link against multiple libcores…</p>



<a name="187221755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding%20tuple%20layout/near/187221755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Köln <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Understanding.20tuple.20layout.html#187221755">(Feb 02 2020 at 21:55)</a>:</h4>
<p>(which could happen if the caller and callee are different libraries)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>